import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import path, { resolve } from 'path';
import { themePreprocessorPlugin } from "@zougt/vite-plugin-theme-preprocessor";
import { visualizer } from "rollup-plugin-visualizer";
import viteCompression from "vite-plugin-compression";
import { Plugin as importToCDN, autoComplete } from 'vite-plugin-cdn-import';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
export default defineConfig({
  plugins: [
    vue(),
    visualizer(),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]',
      customDomId: '__svg__icons__dom__',
    }),
    // 引入插件
    themePreprocessorPlugin({
      less: {
        multipleScopeVars: [
          {
            scopeName: "theme-default",
            path: path.resolve("src/assets/theme/default.less"),
          },
          {
            scopeName: "theme-dark",
            path: path.resolve("src/assets/theme/dark.less"),
          },
          // {
          //   scopeName: "theme-grey",
          //   path: path.resolve("src/assets/theme/grey.less"),
          // },
        ],
      },
    }),
    viteCompression(),
    importToCDN({
      modules: [
        {
          name: 'exceljs',
          var: 'exceljs',
          path: 'https://cdn.bootcdn.net/ajax/libs/exceljs/4.3.0/exceljs.min.js'
        },
        {
          name: 'echarts',
          var: 'echarts',
          path: 'https://cdn.jsdelivr.net/npm/echarts@5.4.0/dist/echarts.min.js'
        },
      ]
    }),
    Components({
      resolvers: [AntDesignVueResolver()]
    })
  ],
  resolve: {
    // 配置路径别名
    alias: {
      '@': resolve(__dirname, './src'),
      'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
    },
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:5173/',
        changeOrigin: true,  // 允许跨域
        rewrite: path => path.replace(/^\/api/, '')
      }
    },
  },
  css: {
    preprocessorOptions: {
      less: {
        // 最高级定义样式
        modifyVars: {
          hack: `true; @import (reference) "${path.resolve("src/assets/css/common.less")}";`,
          //   'primary-color': '#1DA57A',
          //   'link-color': '#1DA57A',
          //   'border-radius-base': '2px',
        },
        javascriptEnabled: true,
      },
    },
  }
})
